package com.codingclub.application.controller;

import com.codingclub.application.convert.SubjectCategoryDTOConverter;
import com.codingclub.application.dto.SubjectCategoryDTO;
import com.codingclub.common.entity.Result;
import com.codingclub.common.enums.ErrCode;
import com.codingclub.common.exception.BusinessException;
import com.codingclub.domain.bo.SubjectCategoryBO;
import com.codingclub.domain.convert.SubjectCategoryConverter;
import com.codingclub.domain.service.SubjectCategoryDomainService;
import com.codingclub.infra.basic.entity.SubjectCategory;
import com.codingclub.infra.basic.service.SubjectCategoryService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import javafx.scene.input.KeyCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.ap.shaded.freemarker.template.utility.StringUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static javafx.scene.input.KeyCode.S;

@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryService categoryService;
    @Resource
    private SubjectCategoryDomainService categoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO dto) {
        //校新增改参数
        dto.validateParam(dto,true);
        SubjectCategoryBO bo = SubjectCategoryDTOConverter.INSTANCE.convertDtoToBo(dto);
        categoryDomainService.add(bo);
        return Result.ok(true);
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO dto) {
        if (dto.getId() == null) {
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }
        SubjectCategory subjectCategory = new SubjectCategory();
        subjectCategory.setId(dto.getId());
        boolean b = categoryService.logicDelete(subjectCategory);
        return Result.ok(b);
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO dto) {
        //校验修改参数
        dto.validateParam(dto, false);
        SubjectCategory subjectCategory = SubjectCategoryDTOConverter.INSTANCE.convertDtoToEntity(dto);
        boolean b = categoryService.update(subjectCategory);
        return Result.ok(b);
    }

    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>>queryPrimaryCategory(@RequestBody SubjectCategoryDTO dto){
        //校验查询参数
        dto.validateQueryParam(dto);
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDtoToBo(dto);
        List<SubjectCategoryBO>boList = categoryDomainService.queryCategory(subjectCategoryBO);
        List<SubjectCategoryDTO>dtoList = SubjectCategoryDTOConverter.INSTANCE.convertBoToDtoList(boList);
        return Result.ok(dtoList);
    }

    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>>queryCategoryByPrimary(@RequestBody SubjectCategoryDTO dto){
        //校验查询参数
        dto.validateQueryParam1(dto);
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDtoToBo(dto);
        List<SubjectCategoryBO>boList = categoryDomainService.queryCategory(subjectCategoryBO);
        List<SubjectCategoryDTO>dtoList = SubjectCategoryDTOConverter.INSTANCE.convertBoToDtoList(boList);
        return Result.ok(dtoList);
    }

}
